<template>
    <div>
        <el-table :data="catelist" border style="width: 100% ;" class="table" row-key="id" default-expand-all
            :tree-props="{ children: 'children' }">
            <el-table-column prop="id" label="分类编号">
            </el-table-column>

            <el-table-column prop="catename" label="分类名称">
            </el-table-column>

            <el-table-column prop="img" label="图片">
                <template slot-scope="item">
                    <div>
                        <img v-if="item.row.img" :src="item.row.img" alt="" class="img">
                    </div>
                </template>
            </el-table-column>

            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        <el-tag type="success" v-if="item.row.status == 1" plain size="medium ">启用</el-tag>
                        <el-tag type="danger" v-else plain size="medium ">禁用</el-tag>
                    </div>
                </template>
            </el-table-column>

            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>

        </el-table>

    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import { catedelete } from "../../request/api"


export default {
    data() {
        return {

        }
    },
    methods: {
        ...mapActions({
            getCateList: "cate/getCateListAction"
        }),
        del(id) {
            // console.log(id);
            //使用elementUI组建库提供的弹窗进行判断用户点击的是确定还是取消
            this.$confirm('是否永久删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                catedelete({ id })
                    .then((res) => {
                        // console.log(res);
                        if (res.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg
                            });
                        }
                    })

                // 重新调用vuex库中的方法
                this.getCateList()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            })

        },
        // 编辑数据
        edit(id) {
            //首先，点击编辑的时候要吊起弹窗
            this.$emit("edit", id)
        }
    },
    mounted() {
        this.getCateList()

    },
    watch: {},
    computed: {
        ...mapGetters({
            catelist: "cate/getCate"
        })
    },
    filters: {},
    components: {}
}
</script>
<style scoped>
.img {
    width: 100px;
    height: 100px;
}
</style>